package site.cyningxu.deviceinfochanger;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Button mDisplay;
    private Button mRandom;
    private Button mSave;
    private EditText mAndroidId;
    private EditText mMacAddress;
    private EditText mBluetoothAddress;
    private EditText mSSID;
    private EditText mBSSID;
    private EditText mDeviceId;
    private EditText mLine1Number;
    private EditText mSimSerialNumber;
    private EditText mSubscriberId;
    private EditText mSimCountryIso;
    private EditText mSimOperator;
    private EditText mSimOperatorName;
    private EditText mNetworkCountryIso;
    private EditText mNetworkOperator;
    private EditText mNetworkOperatorName;
    private EditText mNetworkType;
    private EditText mPhoneType;
    private EditText mSimState;
    private EditText mRADIO;
    private EditText mRELEASE;
    private EditText mSERIAL;
    private EditText mMODEL;
    private EditText mBOARD;
    private EditText mBOOTLOADER;
    private EditText mBRAND;
    private EditText mDEVICE;
    private EditText mHARDWARE;
    private EditText mPRODUCT;

    public void initViews() {
        mDisplay = (Button) findViewById(R.id.display);
        mRandom = (Button) findViewById(R.id.random);
        mSave = (Button) findViewById(R.id.save);
        mAndroidId = (EditText) findViewById(R.id.android_id);
        mMacAddress = (EditText) findViewById(R.id.MacAddress);
        mBluetoothAddress = (EditText) findViewById(R.id.BluetoothAddress);
        mSSID = (EditText) findViewById(R.id.SSID);
        mBSSID = (EditText) findViewById(R.id.BSSID);
        mDeviceId = (EditText) findViewById(R.id.DeviceId);
        mLine1Number = (EditText) findViewById(R.id.Line1Number);
        mSimSerialNumber = (EditText) findViewById(R.id.SimSerialNumber);
        mSubscriberId = (EditText) findViewById(R.id.SubscriberId);
        mSimCountryIso = (EditText) findViewById(R.id.SimCountryIso);
        mSimOperator = (EditText) findViewById(R.id.SimOperator);
        mSimOperatorName = (EditText) findViewById(R.id.SimOperatorName);
        mNetworkCountryIso = (EditText) findViewById(R.id.NetworkCountryIso);
        mNetworkOperator = (EditText) findViewById(R.id.NetworkOperator);
        mNetworkOperatorName = (EditText) findViewById(R.id.NetworkOperatorName);
        mNetworkType = (EditText) findViewById(R.id.NetworkType);
        mPhoneType = (EditText) findViewById(R.id.PhoneType);
        mSimState = (EditText) findViewById(R.id.SimState);
        mRADIO = (EditText) findViewById(R.id.RADIO);
        mRELEASE = (EditText) findViewById(R.id.RELEASE);
        mSERIAL = (EditText) findViewById(R.id.SERIAL);
        mMODEL = (EditText) findViewById(R.id.MODEL);
        mBOARD = (EditText) findViewById(R.id.BOARD);
        mBOOTLOADER = (EditText) findViewById(R.id.BOOTLOADER);
        mBRAND = (EditText) findViewById(R.id.BRAND);
        mDEVICE = (EditText) findViewById(R.id.DEVICE);
        mHARDWARE = (EditText) findViewById(R.id.HARDWARE);
        mPRODUCT = (EditText) findViewById(R.id.PRODUCT);
    }

    private Gson gson = new Gson();
    private SharedPreferences sp;
    private DeviceInfoModel deviceInfoModel;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);
        initViews();
        sp = getSharedPreferences("device_info", MODE_WORLD_READABLE);
        try {
            deviceInfoModel = gson.fromJson(sp.getString("device_info_key", ""), DeviceInfoModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFakeData();
        mRandom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAndroidId.setText(getRandomHexString(16));
                mMacAddress.setText(getRandomMacAddress());
                mBluetoothAddress.setText(getRandomMacAddress());
                mSSID.setText("TP-LINK_" + getRandomHexString(6));
                mBSSID.setText(getRandomMacAddress());
                mDeviceId.setText(getRandomNumberString(15));
                mLine1Number.setText("");
                mSimSerialNumber.setText(getRandomHexString(22));
                mSubscriberId.setText("46002" + getRandomNumberString(10));
                mSimCountryIso.setText("cn");
                mSimOperator.setText("46002");
                mSimOperatorName.setText("CMCC");
                mNetworkCountryIso.setText("cn");
                mNetworkOperator.setText("46000");
                mNetworkOperatorName.setText("中国移动");

                mSimState.setText("5");
                mPhoneType.setText("1");
                mNetworkType.setText("13");

                mRADIO.setText("unknown");
                mRELEASE.setText("5.1");
                mSERIAL.setText(getRandomNumberString(18));
                String s = getRandomModelString();
                mMODEL.setText(s);
                mBOARD.setText("unknown");
                mBOOTLOADER.setText("unknown");
                mBRAND.setText(getRandomBrandString());
                mDEVICE.setText(s);
                mHARDWARE.setText(s);
                mPRODUCT.setText(s);
            }
        });
        mDisplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRealData();
            }
        });
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (deviceInfoModel == null) {
                        deviceInfoModel = new DeviceInfoModel();
                    }
                    if (deviceInfoModel.DeviceInfoHashMap == null) {
                        deviceInfoModel.DeviceInfoHashMap = new HashMap<String, String>();
                    }
                    deviceInfoModel.DeviceInfoHashMap.put("android_id", mAndroidId.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("MacAddress", mMacAddress.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("BluetoothAddress", mBluetoothAddress.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SSID", mSSID.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("BSSID", mBSSID.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("DeviceId", mDeviceId.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("Line1Number", mLine1Number.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SimSerialNumber", mSimSerialNumber.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SubscriberId", mSubscriberId.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SimCountryIso", mSimCountryIso.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SimOperator", mSimOperator.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SimOperatorName", mSimOperatorName.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("NetworkCountryIso", mNetworkCountryIso.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("NetworkOperator", mNetworkOperator.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("NetworkOperatorName", mNetworkOperatorName.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("RADIO", mRADIO.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("RELEASE", mRELEASE.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("SERIAL", mSERIAL.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("MODEL", mMODEL.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("BOARD", mBOARD.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("BOOTLOADER", mBOOTLOADER.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("BRAND", mBRAND.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("DEVICE", mDEVICE.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("HARDWARE", mHARDWARE.getText().toString());
                    deviceInfoModel.DeviceInfoHashMap.put("PRODUCT", mPRODUCT.getText().toString());
                    deviceInfoModel.SimState = Integer.parseInt(mSimState.getText().toString());
                    deviceInfoModel.PhoneType = Integer.parseInt(mPhoneType.getText().toString());
                    deviceInfoModel.NetworkType = Integer.parseInt(mNetworkType.getText().toString());
                    sp.edit().putString("device_info_key", gson.toJson(deviceInfoModel)).commit();
                    Toast.makeText(mContext, "保存成功", Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(mContext, "保存失败", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void setRealData() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        WifiManager wifiManager = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE));
        mAndroidId.setText(Settings.Secure.getString(getContentResolver(), "android_id"));
        mMacAddress.setText(wifiManager.getConnectionInfo().getMacAddress());
        mBluetoothAddress.setText(BluetoothAdapter.getDefaultAdapter().getAddress());
        mSSID.setText(wifiManager.getConnectionInfo().getSSID());
        mBSSID.setText(wifiManager.getConnectionInfo().getBSSID());
        mDeviceId.setText(telephonyManager.getDeviceId());
        mLine1Number.setText(telephonyManager.getLine1Number());
        mSimSerialNumber.setText(telephonyManager.getSimSerialNumber());
        mSubscriberId.setText(telephonyManager.getSubscriberId());
        mSimCountryIso.setText(telephonyManager.getSimCountryIso());
        mSimOperator.setText(telephonyManager.getSimOperator());
        mSimOperatorName.setText(telephonyManager.getSimOperatorName());
        mNetworkCountryIso.setText(telephonyManager.getNetworkCountryIso());
        mNetworkOperator.setText(telephonyManager.getNetworkOperator());
        mNetworkOperatorName.setText(telephonyManager.getNetworkOperatorName());

        mRADIO.setText(Build.RADIO);
        mRELEASE.setText(Build.VERSION.RELEASE);
        mSERIAL.setText(Build.SERIAL);
        mMODEL.setText(Build.MODEL);
        mBOARD.setText(Build.BOARD);
        mBOOTLOADER.setText(Build.BOOTLOADER);
        mBRAND.setText(Build.BRAND);
        mDEVICE.setText(Build.DEVICE);
        mHARDWARE.setText(Build.HARDWARE);
        mPRODUCT.setText(Build.PRODUCT);

        mSimState.setText(telephonyManager.getSimState() + "");
        mPhoneType.setText(telephonyManager.getPhoneType() + "");
        mNetworkType.setText(telephonyManager.getNetworkType() + "");
    }

    private void setFakeData() {
        if (deviceInfoModel == null) {
            return;
        }
        if (deviceInfoModel.DeviceInfoHashMap != null) {
            mAndroidId.setText(deviceInfoModel.DeviceInfoHashMap.get("android_id"));
            mMacAddress.setText(deviceInfoModel.DeviceInfoHashMap.get("MacAddress"));
            mBluetoothAddress.setText(deviceInfoModel.DeviceInfoHashMap.get("BluetoothAddress"));
            mSSID.setText(deviceInfoModel.DeviceInfoHashMap.get("SSID"));
            mBSSID.setText(deviceInfoModel.DeviceInfoHashMap.get("BSSID"));
            mDeviceId.setText(deviceInfoModel.DeviceInfoHashMap.get("DeviceId"));
            mLine1Number.setText(deviceInfoModel.DeviceInfoHashMap.get("Line1Number"));
            mSimSerialNumber.setText(deviceInfoModel.DeviceInfoHashMap.get("SimSerialNumber"));
            mSubscriberId.setText(deviceInfoModel.DeviceInfoHashMap.get("SubscriberId"));
            mSimCountryIso.setText(deviceInfoModel.DeviceInfoHashMap.get("SimCountryIso"));
            mSimOperator.setText(deviceInfoModel.DeviceInfoHashMap.get("SimOperator"));
            mSimOperatorName.setText(deviceInfoModel.DeviceInfoHashMap.get("SimOperatorName"));
            mNetworkCountryIso.setText(deviceInfoModel.DeviceInfoHashMap.get("NetworkCountryIso"));
            mNetworkOperator.setText(deviceInfoModel.DeviceInfoHashMap.get("NetworkOperator"));
            mNetworkOperatorName.setText(deviceInfoModel.DeviceInfoHashMap.get("NetworkOperatorName"));

            mRADIO.setText(deviceInfoModel.DeviceInfoHashMap.get("RADIO"));
            mRELEASE.setText(deviceInfoModel.DeviceInfoHashMap.get("RELEASE"));
            mSERIAL.setText(deviceInfoModel.DeviceInfoHashMap.get("SERIAL"));
            mMODEL.setText(deviceInfoModel.DeviceInfoHashMap.get("MODEL"));
            mBOARD.setText(deviceInfoModel.DeviceInfoHashMap.get("BOARD"));
            mBOOTLOADER.setText(deviceInfoModel.DeviceInfoHashMap.get("BOOTLOADER"));
            mBRAND.setText(deviceInfoModel.DeviceInfoHashMap.get("BRAND"));
            mDEVICE.setText(deviceInfoModel.DeviceInfoHashMap.get("DEVICE"));
            mHARDWARE.setText(deviceInfoModel.DeviceInfoHashMap.get("HARDWARE"));
            mPRODUCT.setText(deviceInfoModel.DeviceInfoHashMap.get("PRODUCT"));
        }

        if (deviceInfoModel.SimState != Integer.MAX_VALUE) {
            mSimState.setText(deviceInfoModel.SimState + "");
        }
        if (deviceInfoModel.PhoneType != Integer.MAX_VALUE) {
            mPhoneType.setText(deviceInfoModel.PhoneType + "");
        }
        if (deviceInfoModel.NetworkType != Integer.MAX_VALUE) {
            mNetworkType.setText(deviceInfoModel.NetworkType + "");
        }
    }

    static ArrayList<String> brandList = new ArrayList<>();

    static {
        brandList.add("google");
        brandList.add("huawei");
        brandList.add("coolpad");
        brandList.add("king");
        brandList.add("hasee");
        brandList.add("asus");
        brandList.add("acer");
        brandList.add("lenovo");
        brandList.add("meitu");
        brandList.add("HTC");
        brandList.add("nubia");
        brandList.add("zuk");
    }

    private static String getRandomBrandString() {
        Collections.shuffle(brandList);
        return brandList.get(0);
    }

    private static String getRandomNumberString(int i) {
        String str = "";
        Random random = new Random();
        int length = "0123456789".length();
        for (int i2 = 0; i2 < i; i2++) {
            str = str + "0123456789".charAt(random.nextInt(length));
        }
        return str;
    }

    private static String getRandomHexString(int i) {
        String str = "";
        Random random = new Random();
        int length = "abcdef0123456789".length();
        for (int i2 = 0; i2 < i; i2++) {
            str = str + "abcdef0123456789".charAt(random.nextInt(length));
        }
        return str;
    }

    private static String getRandomModelString() {
        String str = "";
        Random random = new Random();
        int aL = "QWERTYUIOPASDFGHJKLZXCVBNM".length();
        for (int i2 = 0; i2 < 2; i2++) {
            str = str + "QWERTYUIOPASDFGHJKLZXCVBNM".charAt(random.nextInt(aL));
        }
        str += "-" + "QWERTYUIOPASDFGHJKLZXCVBNM".charAt(random.nextInt(aL))
                + "123456789".charAt(random.nextInt(9))
                + "0123456789".charAt(random.nextInt(10))
                + "0";
        return str;
    }

    private static String getRandomMacAddress() {
        Random random = new Random();
        int length = "abcdef0123456789".length();
        String str = "";
        for (int i = 0; i < 17; i++) {
            if (i % 3 == 2) {
                str = str + ":";
            } else {
                str = str + "abcdef0123456789".charAt(random.nextInt(length));
            }
        }
        return str;
    }
}
